﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EShop_BuyEmailMarketing : System.Web.UI.Page
{
    ManageEmailMarketing mem = new ManageEmailMarketing();
    UsersRepository URepo = new UsersRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        GetEmailTemplateInfo();
        GetEmailPackage();
    }
    void GetEmailTemplateInfo()
    {
        try
        {
            ddlEmailTemplate.DataSource = mem.GetEmailTemplate();
            ddlEmailTemplate.DataTextField = "EmailTemplateName";
            ddlEmailTemplate.DataValueField = "id";
            ddlEmailTemplate.DataBind();
            ddlEmailTemplate.Items.Insert(0, new ListItem("", "-1"));
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    void GetEmailPackage()
    {
        try
        {
            ddlEmailPackage.DataSource = mem.GetEmailPackage();
            ddlEmailPackage.DataTextField = "Point";
            ddlEmailPackage.DataValueField = "id";
            ddlEmailPackage.DataBind();
            ddlEmailPackage.Items.Insert(0, new ListItem("", "-1"));
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    protected void ddlEmailTemplate_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            txtEmailContent.Text = mem.GetEmailContentById(int.Parse(ddlEmailTemplate.SelectedValue));
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            int UserId = URepo.GetUserIdByName(_user["username"]);
            mem.UpdateEmailTemplate(int.Parse(ddlEmailPackage.SelectedValue), int.Parse(ddlEmailTemplate.SelectedValue), UserId);
            lblMessage.Visible = true;
            ddlEmailPackage.SelectedValue = "-1";
            ddlEmailTemplate.SelectedValue = "-1";
            txtEmailContent.Text = string.Empty;
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    
    
}